
import * as API from '@/services';
//权限判断方法
import {hasAnyAuthority, hasAuthority, isSuperAdmin} from "@/utils/auth";
import {AppInitialState} from "@/AppInitialState";


export default (initialState: AppInitialState) => {
  // 在这里按照初始化数据定义项目中的权限，统一管理
  // 参考文档 https://next.umijs.org/docs/max/access
  const isSuperAdmin = () => {
    if (initialState.currentAuthInfo?.principal?.userId == "1") {
      return true;
    }
    return hasAuthority(initialState.currentAuthInfo?.authorities || [], "ROLE_SUPER_ADMIN");
  }


  return {
    //当前用户是否是超级管理员
    isSuperAdmin: isSuperAdmin,
    //是否为指定id用户
    isSameUser: (userId: API.EntityId) => {
      return initialState.currentAuthInfo?.principal.userId == userId;
    },
    //当前用户是否拥有 参数authorities中的任意一项权限
    hasAnyAuthority: (...authorities: string[]) => {
      return isSuperAdmin() || hasAnyAuthority(initialState.currentAuthInfo?.authorities || [], authorities)
    },
    //当前用户是否拥有 参数authority的权限
    hasAuthority: (authority: string) => {
      return isSuperAdmin() || hasAuthority(initialState.currentAuthInfo?.authorities || [], authority)
    },
    getAuthToken: () => {
      return initialState.currentAuthInfo?.token;
    }
  };
};

